package com.ums.upos.util;

import android.content.Context;
import android.content.res.Configuration;

import com.ums.upos.GlobalDataHolder;
import com.ums.upos.uapi.engine.LanguageType;

import java.util.Locale;

/**
 * Created by menha on 2017/8/19.
 */

public class LanguageUtil {
    private Context mOriContext;
    private Context mFirstContext;
    private Context mSecondContext;
    private boolean isDiglossia = false; //是否双语
    public LanguageUtil(Context context){
        mOriContext = mFirstContext = context;
        Configuration config = mOriContext.getResources().getConfiguration();
        int languageType = GlobalDataHolder.getInstance().getLanguageType();
        if(languageType != LanguageType.SIMPLIFIED_CHINESE){
            switch (languageType){
                case LanguageType.TRADITIONAL_CHINESE:
                    config.setLocale(new Locale("zh", "HK"));
                    mFirstContext = mOriContext.createConfigurationContext(config);
                    break;
                case LanguageType.ENGLISH:
                    config.setLocale(new Locale("en"));
                    mFirstContext = mOriContext.createConfigurationContext(config);
                    break;
                case LanguageType.UIGHUR:
                    config.setLocale(new Locale("zh", "TW"));
                    mFirstContext = mOriContext.createConfigurationContext(config);
                    break;
                case LanguageType.UIG_CHN:
                    config.setLocale(new Locale("zh", "TW"));
                    mSecondContext = mOriContext.createConfigurationContext(config);
                    mFirstContext  = mOriContext;
                    setDiglossia(true);
                    break;
                default:
                    GlobalDataHolder.getInstance().setLanguageType(LanguageType.SIMPLIFIED_CHINESE);
                    break;
            }
        }
    }

    public final String getString(int resId){
        if(isDiglossia)
            return mFirstContext.getString(resId) + "\n" + mSecondContext.getString(resId);
        else
            return mFirstContext.getString(resId);
    }

    public final String getFirstString( int resId){
        return mFirstContext.getString(resId);
    }

    public final String getSecondString( int resId) {
        if(!isDiglossia())
            return null;
        return mSecondContext.getString(resId);
    }

    public boolean isDiglossia() {
        return isDiglossia;
    }

    public void setDiglossia(boolean diglossia) {
        isDiglossia = diglossia;
    }
}
